<?php
/**
 * | ---------------------------------------------------------------------------------------------------
 * | Author：johnxu <fsyzxz@163.com>.
 * | ---------------------------------------------------------------------------------------------------
 * | Home: https://www.johnxu.net.
 * | ---------------------------------------------------------------------------------------------------
 * | Data: 2019-03-16
 * | ---------------------------------------------------------------------------------------------------
 * | Desc: 系统设置
 * | ---------------------------------------------------------------------------------------------------
 */

namespace app\admin\controller;

use app\admin\model\CommonLanguage;
use app\admin\model\Config as ConfigModel;
use app\common\controller\Backend;

class System extends Backend
{
    /**
     * 自动回复|问候语
     */
    public function reply()
    {
        if ( $this->request->isPost() )
        {
            $content = $this->request->post( 'content', '', 'htmlspecialchars' );
            $status  = $this->request->post( 'status', 'normal' );

            ConfigModel::setValue( 'reply', [ 'content' => $content, 'status' => $status ] )
                ? $this->success( '保存成功' )
                : $this->error( '保存失败' );
        }
        else
        {
            return $this->fetch( '', [
                'row' => ConfigModel::getValue( 'reply' )
            ] );
        }
    }

    /**
     * 系统配置
     *
     * @return mixed
     */
    public function config()
    {
        if ( $this->request->isPost() )
        {
            $data = $this->request->post( '', null, 'htmlspecialchars' );
            if ( empty( $data['address'] ) )
            {
                $data['address'] = $this->request->server( 'HTTP_HOST' );
            }
            if ( empty( $data['port'] ) )
            {
                $data['port'] = 52915;
            }

            ConfigModel::setValue( 'config', $data ) ? $this->success( '保存成功' ) : $this->error( '保存失败' );
        }
        else
        {
            return $this->fetch( '', [
                'row' => ConfigModel::getValue( 'config' )->value
            ] );
        }
    }

    // common language
    public function language()
    {
        $rows = CommonLanguage::all();

        return $this->fetch( '', [ 'rows' => $rows ] );
    }

    /**
     * 添加|编辑常用语
     *
     * @param null $id
     *
     * @return mixed
     */
    public function add( $id = null )
    {
        $row = CommonLanguage::get( $id );
        if ( $this->request->isPost() )
        {
            $data = $this->request->post();

            if ( $id )
            {
                $row->content = $data['content'];
                $row->status  = $data['status'];
                $result       = $row->save();
            }
            else
            {
                $result = CommonLanguage::create( $data );
            }

            $result !== false ? $this->success( '操作成功', url( 'language' ) ) : $this->error( '操作失败' );
        }
        else
        {
            return $this->fetch( '', [
                'row' => $row
            ] );
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete( int $id )
    {
        CommonLanguage::destroy( $id )
            ? $this->success( '删除成功' )
            : $this->error( '删除失败' );
    }
}
